unit PlayersUpdate;

                                      interface

 uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RzButton, RzStatus, StdCtrls, RzLabel, ExtCtrls, WOT_Base;

 type

  TUpdateForm = class(TForm)
    BtnOK: TRzButton;
    UpdateProgress: TRzProgressStatus;
    E_Status: TRzLabel;
    Timer1: TTimer;

    procedure FormActivate(Sender:TObject);
    procedure SimpleUpdate;
    procedure ThreadUpdate;
    procedure BtnOKClick(Sender:TObject);
    procedure Timer1Timer(Sender:TObject);

  end;

 var UpdateForm: TUpdateForm;

                                     implementation

 {$R *.dfm}

 uses Main;
 
 procedure TUpdateForm.BtnOKClick(Sender:TObject);
  begin
   if BtnOK.Caption='Ok' then
    begin
     ModalResult := mrOk;
     Close;
    end
   else if BtnOK.Caption='Cancel' then BtnOK.Tag := -1;
  end;

 procedure TUpdateForm.FormActivate(Sender:TObject);
  begin
   SimpleUpdate;
  end;

 procedure TUpdateForm.SimpleUpdate;
  var i,n,k : Integer;
      p   : TPlayer;
  begin
   BtnOK.Tag := 0;
   BtnOK.Caption := 'Cancel';
   Timer1.Enabled := False;
   UpdateProgress.Percent := 0;
   E_Status.Caption := '';
   n := TPlayer.Pool.Count;
   k := 0;
   for i:=0 to n-1 do
    begin
     if BtnOK.Tag<>0 then  Break;
     p := TPlayer.Pool.ADR1[i] as TPlayer;
     if MainForm.CB_DontUpdateUnused.Checked and IsUnusedPlayer(p) then Continue;
     E_Status.Caption       := Format('Try to update "%s" (%d of %d)',[p.Name,i+1,n]);
     UpdateProgress.Percent := Round((i+1)/n*100);
     Application.ProcessMessages;
     if p.UpdateHistory(MainForm.CB_AllowSevRecByDay.Checked) then Inc(k);
    end;
   if BtnOK.Tag<>0 then
    begin
     E_Status.Caption := 'Update was stopped';
     Timer1.Enabled := True;
    end
   else
    begin
     UpdateProgress.Percent := 100;
     E_Status.Caption := Format('%d of %d has been updated',[k,n]);
     BtnOK.Caption := 'Ok';
    end;
  end;

 procedure TUpdateForm.ThreadUpdate;
  begin
   // stub
  end;

 procedure TUpdateForm.Timer1Timer(Sender:TObject);
  begin
   if BtnOK.Tag<>0 then
    begin
     Timer1.Enabled := False;
     ModalResult := mrCancel;
     Close;
    end;
  end;

 end.
